package JavaExercise;

import javax.sql.rowset.serial.SerialArray;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class Page17_1 {
    static Scanner sc = new Scanner(System.in);
    static Random rand = new Random();
    public static void main(String[] args) {
        System.out.println("设定一个数组的长度");
        int n = sc.nextInt();
        int [] arr = new int [n];
        arr = SetArr(arr);//获取数组
        for (int x:arr){
            System.out.print(x+",");
        }
        arr = RandArr(arr);//获取打乱数组
        System.out.println();
        for (int x:arr){
            System.out.print(x+",");
        }

    }
    public static int [] SetArr(int [] arr){
        System.out.println("设定这个数列的起始值");
        int f = sc.nextInt();
        System.out.println("设定这个等差数列的差值");
        int c = sc.nextInt();
        for (int i=0;i< arr.length;i++){
            arr[i]=f;
            f+=c;
        }
        return arr;
    }

    public static int [] RandArr(int [] arr){//打乱这个数列
        int r , t;
        for (int i=0;i< arr.length;i++){
            r=rand.nextInt(arr.length);
//            if (r==i){//当两个值相同时因为调用的是同一个数，进行交换操作时必然会等于0
//                i--;
//                break;
//            }
//            arr[i]+=arr[r];
//            arr[r]=arr[i]-arr[r];
//            arr[i]-=arr[r];
            t = arr[i];
            arr [i]=arr[r];
            arr [r] = t;
        }
        return arr;
    }
}
